﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Phoenix.Service;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.Common.Util;
using Phoenix.Enum;
using FluentValidation.Results;
using Phoenix.Common.Helper;
using Microsoft.Security.Application;
namespace AdminSite.Controllers
{
	public class ProductController : Controller
	{
		IAreaService IArea = new AreaService();
		ICategoryService ICategory = new CategoryService();
		IIDCreaterService IIDCreater = new IDCreaterService();
		IProductService IProduct = new ProductService();
		public ActionResult Index()
		{
			IList<ProductModel> model = IProduct.GetAll();
			return View(model);
		}

		[HttpGet]
		public ActionResult Edit(string ID)
		{
			IList<AreaModel> area = IArea.GetAllProvince();
			IList<CategoryModel> category = ICategory.GetByParentID(0);
			ViewData["Category"] = new SelectList(category, "CategoryID", " CategoryName");
			ViewData["Area"] = new SelectList(area, "AreaId", " AreaName");
			if (!string.IsNullOrEmpty(ID))
			{
				ProductModel model = IProduct.GetByProductID(int.Parse(ID));
				return View(model);
			}
			else
			{

				return View();
			}
		}

		[HttpPost]
		[ValidateInput(false)]
		public ActionResult Edit(ProductModel model, HttpPostedFileBase imgFile)
		{
			if (!ModelState.IsValid)
			{
                IList<AreaModel> area = IArea.GetAllProvince();
                IList<CategoryModel> category = ICategory.GetByParentID(0);
                ViewData["Category"] = new SelectList(category, "CategoryID", " CategoryName");
                ViewData["Area"] = new SelectList(area, "AreaId", " AreaName");

				return View(model);
			}

			if (model.ProductID > 0)
			{
				ProductModel oldModel =IProduct.GetByProductID(model.ProductID);
				model.Description = Sanitizer.GetSafeHtmlFragment(model.Description);
				model.CreateTime = oldModel.CreateTime;
				model.UpdateTime = DateTime.Now;
				model.ImagePath = oldModel.ImagePath;
				if (imgFile != null)
				{
					model.ImagePath = ImageHelper.UploadFile(imgFile, "/SysFolder/Product/", model.ProductID);
					ImageHelper.ToThumImages(model.ImagePath, 100, 100);
				}
				IProduct.UpdateByProductID(model);

			}
			else
			{
				model.ProductID = IIDCreater.GetNewID("ProductID", "产品编号", 1000);
				model.Description = Sanitizer.GetSafeHtmlFragment(model.Description);
				model.CreateTime = DateTime.Now;
				model.UpdateTime = DateTime.Parse("1900-01-01");
				model.CategoryIDPath = ICategory.GetByCategoryID(model.CategoryID).IDPath;
				model.AreaIDPath = IArea.GetIDPath(model.AreaID);
				model.ImagePath = "";
				if (imgFile != null)
				{
					model.ImagePath = ImageHelper.UploadFile(imgFile, "/SysFolder/Product/", model.ProductID);
					ImageHelper.ToThumImages(model.ImagePath, 100, 100);
				}
				IProduct.AddProduct(model);
			}

			return Content("保存成功!");
		}

	}
}
